package com.dkd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dkd.common.constant.DkdContants;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.Emp;
import com.dkd.manage.service.IEmpService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;

/**
 * 人员列表Controller
 *
 * @author xwq
 * @date 2024-10-25
 */
@Api(tags = "人员管理接口")
@RestController
@RequestMapping("/manage/emp")
public class EmpController extends BaseController {

    @Autowired
    private IEmpService empService;

    @Autowired
    private IVendingMachineService vendingMachineService;

    /**
     * 查询人员列表
     */
    @ApiOperation("查询人员列表")
    @PreAuthorize("@ss.hasPermi('manage:emp:list')")
    @GetMapping("/list")
    public TableDataInfo list(
            @ApiParam(value = "查询条件") Emp emp) {
        startPage();
        List<Emp> list = empService.selectEmpList(emp);
        return getDataTable(list);
    }

    /**
     * 导出人员列表
     */
    @ApiOperation("导出人员列表")
    @PreAuthorize("@ss.hasPermi('manage:emp:export')")
    @Log(title = "人员列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(
            HttpServletResponse response,
            @ApiParam(value = "查询条件") Emp emp) {
        List<Emp> list = empService.selectEmpList(emp);
        ExcelUtil<Emp> util = new ExcelUtil<>(Emp.class);
        util.exportExcel(response, list, "人员列表数据");
    }

    /**
     * 获取人员详细信息
     */
    @ApiOperation("获取人员详细信息")
    @PreAuthorize("@ss.hasPermi('manage:emp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(
            @ApiParam(value = "人员ID", required = true) @PathVariable("id") Long id) {
        return success(empService.selectEmpById(id));
    }

    /**
     * 新增人员
     */
    @ApiOperation("新增人员")
    @PreAuthorize("@ss.hasPermi('manage:emp:add')")
    @Log(title = "人员列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(
            @ApiParam(value = "新增人员信息", required = true) @RequestBody Emp emp) {
        return toAjax(empService.insertEmp(emp));
    }

    /**
     * 修改人员
     */
    @ApiOperation("修改人员信息")
    @PreAuthorize("@ss.hasPermi('manage:emp:edit')")
    @Log(title = "人员列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(
            @ApiParam(value = "修改人员信息", required = true) @RequestBody Emp emp) {
        return toAjax(empService.updateEmp(emp));
    }

    /**
     * 删除人员
     */
    @ApiOperation("删除人员")
    @PreAuthorize("@ss.hasPermi('manage:emp:remove')")
    @Log(title = "人员列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @ApiParam(value = "删除人员的ID数组", required = true) @PathVariable Long[] ids) {
        return toAjax(empService.deleteEmpByIds(ids));
    }

    /**
     * 根据售货机获取运营人员列表
     */
    @ApiOperation("根据售货机编号获取运营人员列表")
    @PreAuthorize("@ss.hasPermi('manage:emp:list')")
    @GetMapping("/businessList/{innerCode}")
    public AjaxResult businessList(
            @ApiParam(value = "售货机编号", required = true) @PathVariable("innerCode") String innerCode) {
        VendingMachine vm = vendingMachineService.selectVendingMachineByInnerCode(innerCode);
        if (vm == null) {
            return error();
        }
        Emp empParam = new Emp();
        empParam.setRegionId(vm.getRegionId());
        empParam.setStatus(DkdContants.EMP_STATUS_NORMAL);
        empParam.setRoleCode(DkdContants.ROLE_CODE_BUSINESS);
        return success(empService.selectEmpList(empParam));
    }

    /**
     * 根据售货机获取运维人员列表
     */
    @ApiOperation("根据售货机编号获取运维人员列表")
    @PreAuthorize("@ss.hasPermi('manage:emp:list')")
    @GetMapping("/operationList/{innerCode}")
    public AjaxResult getOperationList(
            @ApiParam(value = "售货机编号", required = true) @PathVariable("innerCode") String innerCode) {
        VendingMachine vm = vendingMachineService.selectVendingMachineByInnerCode(innerCode);
        if (vm == null) {
            return error("售货机不存在");
        }
        Emp empParam = new Emp();
        empParam.setRegionId(vm.getRegionId());
        empParam.setStatus(DkdContants.EMP_STATUS_NORMAL);
        empParam.setRoleCode(DkdContants.ROLE_CODE_OPERATOR);
        return success(empService.selectEmpList(empParam));
    }
}
